﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DMS.Business.WebControls
{
    public class ExtendTemplateField : TemplateField 
    {
        public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
        {

            base.InitializeCell(cell, cellType, rowState, rowIndex);
            ITemplate headerTemplate = null;
            switch (cellType)
            {
                case DataControlCellType.Header:
                    headerTemplate = this.HeaderTemplate;
                    break;

                case DataControlCellType.Footer:
                    headerTemplate = this.FooterTemplate;
                    break;

                case DataControlCellType.DataCell:
                    headerTemplate = this.ItemTemplate;
                    if ((rowState & DataControlRowState.Edit) == DataControlRowState.Normal)
                    {
                        if ((rowState & DataControlRowState.Insert) != DataControlRowState.Normal)
                        {
                            if (this.InsertItemTemplate != null)
                            {
                                headerTemplate = this.InsertItemTemplate;
                            }
                            else if (this.EditItemTemplate != null)
                            {
                                headerTemplate = this.EditItemTemplate;
                            }
                        }
                        else if (((rowState & DataControlRowState.Alternate) != DataControlRowState.Normal) && (this.AlternatingItemTemplate != null))
                        {
                            headerTemplate = this.AlternatingItemTemplate;
                        }
                        break;
                    }
                    if (this.EditItemTemplate != null)
                    {
                        headerTemplate = this.EditItemTemplate;
                    }
                    break;
            }
            if (headerTemplate != null )
            {
                cell.Text = string.Empty;                
                headerTemplate.InstantiateIn(cell);
                if (cellType == DataControlCellType.Header)
                {
                    this.HeaderText = cell.Text;
                    base.InitializeCell(cell, cellType, rowState, rowIndex);
                }
            }
            else if (cellType == DataControlCellType.DataCell)
            {
                cell.Text = "&nbsp;";
            }
        }


    }
}
